﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Identitycore.Serivces;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Identitycore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        /// <summary>
        ///构造函数    Collection 容器
        /// </summary>
        /// <param name="services">IServiceCollection 本身就是给依赖注入的容器 </param>
        public void ConfigureServices(IServiceCollection services)
        {
            ///开启Cookie授权过滤器
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                o =>
                {
                    o.LoginPath = new PathString("/Login/Index");

                });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            AddYiLaiZhuRu(services);
            services.AddDbContextPool<MyDbContext>(option =>
            option.UseSqlServer(Configuration.GetConnectionString("Xcode"))
             );
        }
        /// <summary>
        /// 添加依赖注入
        /// </summary>
        /// <param name="services"></param>
        private static void AddYiLaiZhuRu(IServiceCollection services)
        {
            //创建MyService对象给IService接口使用
            //每个调用都创建一个实例
            services.AddTransient<IService, MyService>();
            //一个请求域只有一个实例 
            services.AddScoped<IService, MyService>();
            //单例模式 只要系统运行 那么就只有这一个实例
            services.AddSingleton<IService, MyService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            //启用授权
            app.UseAuthentication();
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
